package lumingweihua.future.cn.lumingweihua.network;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import cn.jpush.android.api.JPushInterface;
import lumingweihua.future.cn.lumingweihua.AppApplication;
import lumingweihua.future.cn.lumingweihua.MainActivity;
import lumingweihua.future.cn.lumingweihua.person.LoginActivity;
import lumingweihua.future.cn.lumingweihua.ui.message.SystemNotifyActivity;

/**
 * Created by z1986 on 2018/3/24.
 */

public class JPushReceiver extends BroadcastReceiver {
    NotificationManager nm;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (nm == null) {
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            //JPush用户注册成功
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            //接受到推送下来的自定义消息
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            //"接受到推送下来的通知
//            String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
//            String message = bundle.getString(JPushInterface.EXTRA_ALERT);
//            String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            //用户点击打开了通知
            Intent[] intents = new Intent[2];
            if (!AppApplication.getInstance().isUserLogin()) {
                intents[0] = new Intent(context, LoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            } else {
                intents[0] = new Intent(context, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            }
            intents[1] = new Intent(context, SystemNotifyActivity.class);
            context.startActivities(intents);
        } else {
            //Unhandled intent
        }
    }
}
